package com.console.operations.planmgr;

import java.util.List;

import org.openqa.selenium.By;
import org.openqa.selenium.WebElement;

import com.common.test.GetDataFromImportFile;
import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.common.util.GetTempleteFileUtil;
import com.console.pages.planmgr.FinancePlanDepositPage;

public class OhgFinancePlanFrontRelease extends OperationBase {

	public OhgFinancePlanFrontRelease(UITestBase tb){
		super(tb);
	}
	
	public boolean isstatuscorrected = false;
	
	public void release() throws Exception{
		switchToDefaultContent();
		FinancePlanDepositPage fpdp = new FinancePlanDepositPage(tb);
		//防止页面刷新导致点击报错的处理
		elementToBeClickable(fpdp.panelheaddiv);
		//到新页面后，直到投资链接可视
		waitForElementDisplayed(fpdp.panelheaddiv);
		String financeplantitle = GetDataFromImportFile.getOneLineDataFromCell(GetTempleteFileUtil.INVESTBATCH_OHG_TEMPLATE, "名称");
		//动态设置DefaultElement的findValue
		fpdp.frontreleaseLnk.setfindValue("//tr[td[text()='"+ financeplantitle +"']]/td[9]/a[2]");
		elementToBeClickable(fpdp.pagination);
		waitForElementDisplayed(fpdp.pagination);
		//得到总页数控件下tag为li的子控件集合
		List<WebElement> subelement = fpdp.pagination.getNativeElement().findElements(By.tagName("li"));
		int pagecount = 0;
		System.out.println("text is " + subelement.get(subelement.size()-1).getText());
		//得到分页的页数
		if(subelement.size()>1){
			pagecount = Integer.parseInt(subelement.get(subelement.size()-1).getText());
		}else if(subelement.size()==1){
			pagecount = 1;
		}
		log.info("page count is " + pagecount);
		if(pagecount>1){
			for(int i=1;i<pagecount;i++){
				if(isPageContainsStr(financeplantitle)){
					waitForElementExist(fpdp.frontreleaseLnk);
					fpdp.frontreleaseLnk.click();
					break;
				}else{
					fpdp.pageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					fpdp.pageLink.click();
					pause(1000);
				}
			}
		}else{
			waitForElementDisplayed(fpdp.frontreleaseLnk);
			fpdp.frontreleaseLnk.click();
		}
		pause(1000);
		String dcstatus = DBUtil.queryOneCell("select dc_status from fiz_plan where vc_name = ?", new Object[]{financeplantitle});
		isstatuscorrected = dcstatus.equals("01");
		if(!isstatuscorrected){
			log.error("点击前台发布后状态没有变为01（前台筹集中）");
			return;
		}
	}
}
